package com.itcast.demoone

import android.os.Bundle
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import com.itcast.demoone.fragment.DetailFragment
import com.itcast.demoone.fragment.ListFragment

class MainActivity : AppCompatActivity(), ListFragment.OnItemClickListener {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_main)

        // 创建并加载Fragment(只加载一次)
        /**
         * 为 null 时，表示 Activity 是首次创建;
         * 不为 null 时，表示 Activity 不是首次创建，即返回栈中存在前一个Activity实例
         */
        if (savedInstanceState == null) {
            supportFragmentManager.beginTransaction()
                .add(R.id.fragment_container, ListFragment())
                .commit()
        }

    }

    override fun onItemClick(itemId: Int) {
        supportFragmentManager.beginTransaction()
            .replace(R.id.fragment_container, DetailFragment())
            .addToBackStack(null) // 添加返回栈
            .commit()
    }
}
